package com.hxk.tx.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hxk.tx.pojo.User;
import com.hxk.tx.service.UserService;
import com.hxk.tx.service.impl.UserServiceImpl;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
	
	private UserService userServletImpl;
	@Value("${sys.sysName}")//获取属性文件中的值
	private String sysName;
	
	@Override
	public void init() throws ServletException {
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		userServletImpl = wac.getBean("usersService", UserServiceImpl.class);
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		System.out.println(sysName);
		User rsUser = userServletImpl.selectByUser(user);
		if(rsUser != null){
			res.sendRedirect("main.jsp");
		}else{
			res.sendRedirect("login.jsp");
		}
	}
}
